<template>
  <el-dialog
    width="70%"
    :title="dataForm.title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-row  type="flex" :gutter="10" class="row-bg">
      <el-col :span="5">
        <div style="margin-top: 15px">
          <el-tree
            ref="tree"
            :props="props1"
            :data="data"
            node-key="id"
            :expand-on-click-node="false"
            @node-click="select">
      <span class="custom-tree-node" slot-scope="{ node, data }" v-on:mouseenter="showEdit(node)"
            v-on:mouseleave="hideEdit(node)">
          <span style="display: inline-block;padding-right: 30px"><icon-svg name="fileopen"></icon-svg>&nbsp;<span v-if="node.data.select" style="color: red">{{ node.label}}</span><span
            v-if="!node.data.select">{{ node.label}}</span></span>
      </span>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="19">
        <div class="mod-config">
          <el-form :inline="true" size="mini" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item label="上传用户">
              <el-select clearable
                         v-model="selectUser"
                         placeholder="选择筛选"
                         @change="getDataList"
                         @clear="getDataList"
              >
                <el-option
                  v-for="item in users"
                  :key="item.userId"
                  :label="item.username"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="getDataList()">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addOrUpdateHandle()">确定</el-button>
            </el-form-item>
          </el-form>
          <el-table
            ref="multipleTable"
            height="320"
            row-key="id"
            size="mini"
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%;">
            <el-table-column
              fixed
              type="selection"
              header-align="center"
              align="center"
              reserve-selection
              width="50">
            </el-table-column>
            <!--<el-table-column-->
              <!--prop="id"-->
              <!--header-align="center"-->
              <!--align="center"-->
              <!--label="ID"-->
              <!--width="50">-->
            <!--</el-table-column>-->
            <el-table-column
              prop="name"
              header-align="center"
              align="center"
              label="名称">
            </el-table-column>
            <el-table-column
              prop="cid"
              header-align="center"
              align="center"
              label="目录">
              <template slot-scope="scope">
                <span style="">
                  {{scope.row.cName}}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="fileName"
              header-align="center"
              align="center"
              label="文件名"
              width="100">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="left">
                  <p>{{ scope.row.fileName }}</p>
                  <div slot="reference" class="name-wrapper">
                    <span>{{ scope.row.fileName.length > 10?scope.row.fileName.substring(0,10) + '...':scope.row.fileName }}</span>
                  </div>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
              prop="filePath"
              header-align="center"
              align="center"
              label="路径"
              width="100">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="left">
                  <p>{{ scope.row.filePath }}</p>
                  <div slot="reference" class="name-wrapper">
                    <span>{{ scope.row.filePath.length > 10?scope.row.filePath.substring(0,10) + '...':scope.row.filePath }}</span>
                  </div>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
              prop="createUser"
              header-align="center"
              align="center"
              label="创建人">
              <template slot-scope="scope">
                <span style="">
                  {{scope.row.uName}}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              header-align="center"
              align="center"
              label="创建时间">
            </el-table-column>
            <el-table-column
              prop="fileSize"
              header-align="center"
              align="center"
              label="大小">
              <template slot-scope="scope">
                <span style="color: green">{{scope.row.fileSize/1000}}</span>KB
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            small
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, prev, pager, next">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
  import AddOrUpdate from './material-add-or-update'

  export default {
    name: 'add-play',
    data () {
      return {
        date_value: [new Date(), new Date()],
        visible: true,
        macs: [],
        selectUser: null,
        selectStatus: null,
        users: [],
        dataForm: {
          key: '',
          instal: 0,
          title: ''
        },
        props1: {
          label: 'name',
          children: 'children',
          isLeaf: 'leaf'
        },
        data: [],
        showAddGroup: false,
        showEditGroup: false,
        curNode: {},
        selectedNode: {},
        pNode: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
    },
    mounted () {
      this.getGroups()
      this.$http({
        url: this.$http.adornUrl('/sys/user/l'),
        method: 'get'

      }).then(({data}) => {
        this.users = data.data
      })
    },
    methods: {
      init (ids, dataType) {
        if (dataType === 1) {
          this.dataForm.title = '远程安装'
        } else {
          this.dataForm.title = '远程卸载'
        }
        this.macs = ids
        this.dataForm.instal = dataType
        this.visible = true
        this.dataListSelections = []
        this.getDataList()
        this.$nextTick(() => {
          this.$refs.multipleTable.clearSelection()
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/material/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'cid': this.selectedNode.data ? this.selectedNode.data.id : 6,
            'createUser': this.selectUser
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 安装，卸载
      addOrUpdateHandle () {
        if (this.dataListSelections.length === 0 || this.dataListSelections.length > 1) {
          this.$message.error('请选择一个应用素材')
          return
        }
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        var remoteInstall = {}
        remoteInstall.macs = this.macs
        remoteInstall.mids = ids
        remoteInstall.installType = this.dataForm.instal
        console.log(remoteInstall)
        this.$http({
          url: this.$http.adornUrl('/generator/stb/remoteInstall'),
          method: 'post',
          data: this.$http.adornData(remoteInstall, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      getGroups () {
        const self = this
        const url = '/sys/materialclass/findSelfAndSon?id=6'
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            self.data = data.data
          } else {
            // alert(data.msg)
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      },
      saveGroup (callback) {
        const node = this.curNode
        const url = node.id == null ? '/sys/materialclass/save' : '/sys/materialclass/update'
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'post',
          data: this.$http.adornData(node)
        }).then(({data}) => {
          if (data && data.code === 0) {
            callback(data)
          } else {
            // alert(data.msg)
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      },
      addRoot () {
        this.curNode = {}
        this.curNode.pid = 0
        this.showAddGroup = true
      },
      saveRoot () {
        const self = this
        this.saveGroup(function (r) {
          console.log('r', r)
          self.showAddGroup = false
          self.append(r.data)
        })
      },
      getRoot (node) {
        if (node.data.pid === 0) {
          return node.data.id
        } else {
          return this.getRoot(this.$refs.tree.getNode(node.data.pid))
        }
      },
      reload: function (event) {
        this.getDataList()
      },
      append (data) {
        data.children = []
        if (data.pid === 0) {
          this.data.push(data)
        } else {
          this.$refs.tree.append(data, this.pNode)
        }
      },
      addNode (node) {
        this.pNode = node
        this.curNode = {}
        this.curNode.pid = node.data.id
        this.showAddGroup = true
      },
      remove (node, data) {
        this.$refs.tree.remove(node)
      },
      edit (node) {
        this.curNode = node.data
        this.showEditGroup = true
      },
      saveEdit () {
        const self = this
        this.saveGroup(function (r) {
          self.showEditGroup = false
        })
      },
      hideEdit (node) {
        node.data.s_edit = false
        node.data.name = node.data.name.trim()
      },
      showEdit (node) {
        node.data.s_edit = true
        node.data.name = node.data.name + ' '
      },
      select (data, node, ele) {
        if (this.selectedNode.data) {
          // console.log(this.selectedNode)
          this.selectedNode.data.select = false
          this.selectedNode.data.name = ' ' + this.selectedNode.data.name
          this.selectedNode.data.name = this.selectedNode.data.name.trim()
        }
        data.select = true
        data.name = ' ' + node.data.name
        this.selectedNode = node
        this.getDataList()
      }
    }
  }
</script>
